package dk.liljedahl.christian.apx.junk;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.*;

import dk.liljedahl.christian.apx.simpleexample.ProcessingPlant;

public class SwtArduinoUI /*implements Runnable*/ {

	private static Combo combo;
	private static Text text;

	ProcessingProcess myParent; 

	public ProcessingPlant processingPlant = new ProcessingPlant(); 
	public Thread processingPlantThread = new Thread(processingPlant);
	public Display display = new Display ();
	
/*	public SwtArduinoUI(ProcessingProcess myPapa) {
		// we need to establish a reference to the Processing Sketch, to make it possible to talk to it
		this.myParent = myPapa;
	}
*/
/*	@Override
	public void run() {*/
	public static void main(String[] args) {
		SwtArduinoUI swtArduinoUI = new SwtArduinoUI();
		swtArduinoUI.runner();
	}
	public void runner() {

		// TODO Auto-generated method stub
		Shell shell = new Shell (display);
		shell.setLayout (new RowLayout ());
		combo = new Combo (shell, SWT.NONE);
		
		display.syncExec(processingPlantThread);
		//swtArduinoUI.processingPlantThread.start();
		
		Button buttonSerial = new Button(shell, SWT.PUSH);
		buttonSerial.setText("Get serial ports");
		buttonSerial.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				// Get the Serial Ports and fill them in the combo box
				//ProcessingPlant pp = new ProcessingPlant();
				String[] l = processingPlant.SerialPorts();
				combo.setItems(l);
				combo.add("Ports",0);
				combo.select(0);

			}
		});

		
		text = new Text (shell, SWT.SINGLE | SWT.BORDER);
		text.setText ("some text");
		combo.addListener (SWT.Selection, new Listener () {
			public void handleEvent (Event e) {
				// We react on everything but the "Ports" if selected
				Combo c = (Combo) e.widget;
				String t = c.getText();
				if(!t.equals("Ports")){
					text.setText(t);
				}
			}
		});

		shell.open();
    	
    	// Default Swt-stuff
		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ()) display.sleep ();
		}
		display.dispose ();

	}

}
